package com.weixing.mall.provider.controller.front;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.core.utils.CommonUtil;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.enums.WhetherEnum;
import com.weixing.mall.provider.model.vo.ApplyWithdrawVo;
import com.weixing.mall.provider.model.vo.FrontAgentWithdrawSettleVo;
import com.weixing.mall.provider.service.IAgentService;
import com.weixing.mall.provider.service.IAgentWithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wx
 * @Date 2020/1/7 10:12
 * @Version 1.0
 */
@RestController
@RequestMapping("front/agentWithdraw")
@Api(value = "前端:提现",tags = "前端:提现api")
public class FrontAgentWithdrawController extends BaseController {
    @Autowired
    private IAgentWithdrawService iAgentWithdrawService;
    @Autowired
    private IAgentService iAgentService;
    @ApiOperation("提现信息查询")
    @GetMapping("withdrawInfo")
    public Result withdrawInfo(@MemInfo MemAuth auth, String currentTime){
        FrontAgentWithdrawSettleVo frontAgentWithdrawSettleVo=iAgentWithdrawService.withdrawInfo(currentTime,auth.getAbid());
        return ResultUtil.success(frontAgentWithdrawSettleVo);
    }


    @ApiOperation("申请提现")
    @GetMapping("applyWithdraw")
    public Result applyWithdraw(@MemInfo MemAuth auth,ApplyWithdrawVo applyWithdrawVo){
        //先判断提现密码是否一致
        long agentId = auth.getAbid();
        Agent agent = iAgentService.getById(agentId);
        if(WhetherEnum.NO.getValue().equals(applyWithdrawVo.getWhether().getValue())){
            if(CommonUtil.bCryptCheck(applyWithdrawVo.getPwd(), agent.getWithdrawPwd())){
                iAgentWithdrawService.applyWithdraw(applyWithdrawVo,agentId);
                return ResultUtil.success();
            }else {
                return ResultUtil.error("提现密码错误");
            }
        }else {
            if(agent!=null){
                String s = CommonUtil.encryptPassword(applyWithdrawVo.getPwd());
                Agent agent1 = new Agent();
                agent1.setId(agentId);
                agent1.setWithdrawPwd(s);
                iAgentService.updateById(agent1);
                return ResultUtil.success();
            }else {
                return ResultUtil.error("用户不存在");
            }
        }

    }

    @ApiOperation(value = "代理商提现记录查看")
    @GetMapping("withdrawRecord")
    public Result withdrawRecord(@MemInfo MemAuth auth){
        return ResultUtil.success(iAgentWithdrawService.withdrawRecord(initMpPage(),auth.getAbid()));
    }
//    @ApiOperation("设置提现密码")
//    @PostMapping("setWithdrawPwd")
//    public Result setWithdrawPwd(String pwd){
//        long currentUserId = RequestUtil.getCurrentUserId();
//        Agent agent = iAgentService.getById(currentUserId);
//        if(agent==null){
//            return ResultUtil.error("用户不存在");
//        }else {
//            String s = CommonUtil.encryptPassword(pwd);
//            Agent agent1 = new Agent();
//            agent1.setId(currentUserId);
//            agent1.setWithdrawPwd(s);
//            iAgentService.updateById(agent1);
//            return ResultUtil.success();
//        }
//
//    }

    @ApiOperation("申请提现页—历史银行信息")
    @GetMapping("bankHistory")
    public Result bankHistory(@MemInfo MemAuth auth){
        return ResultUtil.success(iAgentWithdrawService.selectBankHistory(auth.getAbid()));
    }
}
